"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _jsxRuntime = require("react/jsx-runtime");

var _icons = require("@ant-design/icons");

var _proProvider = require("@ant-design/pro-provider");

var _antdV = require("antd-v4");

var _classnames = _interopRequireDefault(require("classnames"));

var _react = _interopRequireWildcard(require("react"));

require("./index.less");

var FieldLabel = function FieldLabel(props, ref) {
  var _classNames;

  var label = props.label,
      onClear = props.onClear,
      value = props.value,
      _props$size = props.size,
      size = _props$size === void 0 ? 'middle' : _props$size,
      disabled = props.disabled,
      onLabelClick = props.onLabelClick,
      ellipsis = props.ellipsis,
      placeholder = props.placeholder,
      className = props.className,
      style = props.style,
      formatter = props.formatter,
      bordered = props.bordered,
      _props$allowClear = props.allowClear,
      allowClear = _props$allowClear === void 0 ? true : _props$allowClear;

  var _useContext = (0, _react.useContext)(_antdV.ConfigProvider.ConfigContext),
      getPrefixCls = _useContext.getPrefixCls;

  var prefixCls = getPrefixCls('pro-core-field-label');
  var intl = (0, _proProvider.useIntl)();
  var clearRef = (0, _react.useRef)(null);
  var labelRef = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      labelRef: labelRef,
      clearRef: clearRef
    };
  });

  var formatterText = function formatterText(aValue) {
    if (formatter) {
      return formatter(aValue);
    }

    return Array.isArray(aValue) ? aValue.join(',') : aValue;
  };

  var getTextByValue = function getTextByValue(aLabel, aValue) {
    if (aValue !== undefined && aValue !== null && aValue !== '' && (!Array.isArray(aValue) || aValue.length)) {
      var _str$toString, _str$toString$substr;

      var prefix = aLabel ? (0, _jsxRuntime.jsxs)("span", {
        onClick: onLabelClick,
        className: "".concat(prefixCls, "-text"),
        children: [aLabel, ': ']
      }) : '';
      var str = formatterText(aValue);

      if (!ellipsis) {
        return (0, _jsxRuntime.jsxs)("span", {
          style: {
            display: 'inline-flex',
            alignItems: 'center'
          },
          children: [prefix, formatterText(aValue)]
        });
      } // 普通表单值最大长度41，如2022-06-21 20:11:25 ~ 2022-06-22 20:11:25


      var VALUE_MAX_LENGTH = 41;

      var getText = function getText() {
        var isArrayValue = Array.isArray(aValue) && aValue.length > 1;
        var unitText = intl.getMessage('form.lightFilter.itemUnit', '项');

        if (typeof str === 'string' && str.length > VALUE_MAX_LENGTH && isArrayValue) {
          return "...".concat(aValue.length).concat(unitText);
        }

        return '';
      };

      var tail = getText();
      return (0, _jsxRuntime.jsxs)("span", {
        title: typeof str === 'string' ? str : undefined,
        style: {
          display: 'inline-flex',
          alignItems: 'center'
        },
        children: [prefix, (0, _jsxRuntime.jsx)("span", {
          style: {
            paddingLeft: 4
          },
          children: typeof str === 'string' ? str === null || str === void 0 ? void 0 : (_str$toString = str.toString()) === null || _str$toString === void 0 ? void 0 : (_str$toString$substr = _str$toString.substr) === null || _str$toString$substr === void 0 ? void 0 : _str$toString$substr.call(_str$toString, 0, VALUE_MAX_LENGTH) : str
        }), tail]
      });
    }

    return aLabel || placeholder;
  };

  return (0, _jsxRuntime.jsxs)("span", {
    className: (0, _classnames.default)(prefixCls, "".concat(prefixCls, "-").concat(size), (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "-active"), !!value || value === 0), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "-disabled"), disabled), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "-bordered"), bordered), (0, _defineProperty2.default)(_classNames, "".concat(prefixCls, "-allow-clear"), allowClear), _classNames), className),
    style: style,
    ref: labelRef,
    children: [getTextByValue(label, value), (value || value === 0) && allowClear && (0, _jsxRuntime.jsx)(_icons.CloseOutlined, {
      role: "button",
      title: "\u6E05\u9664",
      className: (0, _classnames.default)("".concat(prefixCls, "-icon"), "".concat(prefixCls, "-close")),
      onClick: function onClick(e) {
        if (onClear && !disabled) {
          onClear();
        }

        e.stopPropagation();
      },
      ref: clearRef
    }), (0, _jsxRuntime.jsx)(_icons.DownOutlined, {
      className: (0, _classnames.default)("".concat(prefixCls, "-icon"), "".concat(prefixCls, "-arrow"))
    })]
  });
};

var _default = /*#__PURE__*/_react.default.forwardRef(FieldLabel);

exports.default = _default;